package app.beit.fintrack.controller.search.rep;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import app.beit.fintrack.bean.HospitalBean;
import app.beit.fintrack.bean.summary.SummaryRepDataBean;
import app.beit.fintrack.controller.BaseController;
import app.beit.fintrack.controller.ResponseFintrack;
import app.beit.fintrack.controller.bean.SummaryRepSessionBean;
import app.beit.fintrack.service.MasterService;
import app.beit.fintrack.service.SummaryRepService;
import app.beit.fintrack.utils.DateFormatUtils;
import app.beit.fintrack.utils.constr.MsgConstr;
import app.beit.fintrack.utils.constr.SessionConstr;

@Controller
@RequestMapping("/summaryrep")
public class SummaryTrackREPController extends BaseController {
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private MasterService masterService;
	
	@Autowired
	private SummaryRepService summaryRepService;

	@RequestMapping(method=RequestMethod.GET)
	public String init(){
		return "summaryrep/summaryrep";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getByHCode", method=RequestMethod.POST)
	public @ResponseBody ResponseFintrack<SummaryRepSessionBean> getByHcode(final HttpServletRequest request, @RequestParam("start") final int start, 
			@RequestParam("end") final int end, @RequestParam("fromDate") final String fromDate, @RequestParam("toDate") final String toDate, 
			@RequestParam("hCode") final String hCode, @RequestParam("authType") final String authType){
		this.log.info("start search get by hospital code.");
		ResponseFintrack<SummaryRepSessionBean> response = null;
		SummaryRepSessionBean srsb = (SummaryRepSessionBean) super.getAttributeInSession(request, SessionConstr.SESSION_SUMMARY_REP);
		srsb = srsb == null ? new SummaryRepSessionBean() : srsb;
		
		try {
			srsb.setProvince(this.masterService.getProvinceByHCode(hCode));
			srsb.setZone(this.masterService.getZoneByHCode(hCode));
			srsb.setHospital(this.masterService.getHospitalByHCode(hCode));
			
			final Date fd = DateFormatUtils.strToDate(fromDate);
			final Date td = DateFormatUtils.strToDate(toDate);
			srsb.setFromDate(fd);
			srsb.setFromDateStr(fromDate);
			srsb.setToDate(td);
			srsb.setToDateStr(toDate);
			srsb.setAuthType(authType);

			BigDecimal allPerson = new BigDecimal(0);
			/*BigDecimal appeal = new BigDecimal(0);
			BigDecimal notPass = new BigDecimal(0);
			BigDecimal notPassDeny = new BigDecimal(0);
			BigDecimal pass = new BigDecimal(0);*/
			
			/*appeal = this.summaryRepService.getCountPerson(hCode, fd, td, ESearchByStatus.APPEAL, authType);
			notPass = this.summaryRepService.getCountPerson(hCode, fd, td, ESearchByStatus.NOT_PASS, authType);
			notPassDeny = this.summaryRepService.getCountPerson(hCode, fd, td, ESearchByStatus.NOT_PASS_C, authType);
			pass = this.summaryRepService.getCountPerson(hCode, fd, td, ESearchByStatus.PASS, authType);*/
			allPerson = this.summaryRepService.getCountPerson(hCode, fd, td, null, authType);
			
			//final BigDecimal actAmount = this.summaryRepService.getSumsActAmount(hCode, fd, td, authType);
			
			final List<SummaryRepDataBean> results = this.summaryRepService.getDetailsByRepNo(hCode, fd, td, start, end, authType);

			srsb.setCountAllPerson(allPerson);
			//srsb.setCountAppeal(appeal);
			//srsb.setCountNotPass(notPass);
			//srsb.setCountNotPassDeny(notPassDeny);
			//srsb.setCountPass(pass);
			//srsb.setActAmount(actAmount);
			//srsb.setTotal(this.summaryRepService.getSumsTotal(hCode, fd, td, authType));
			srsb.setRepDataBeans(results);
			
			response = super.responseSuccess();
			response.setDatas(srsb);
			
			request.getSession().setAttribute(SessionConstr.SESSION_SUMMARY_TRACK, srsb);
		} catch (Exception e) {
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		
		return response;
	}

	@RequestMapping(value="/{hcode}", method=RequestMethod.GET)
	public ModelAndView exportXLS(final HttpServletRequest request, final HttpServletResponse response){
		final SummaryRepSessionBean stsb = (SummaryRepSessionBean) super.getAttributeInSession(request, SessionConstr.SESSION_SUMMARY_TRACK);
		try{
			final Date fd = stsb.getFromDate();
			final Date td = stsb.getToDate();
			String hCode = "";
			final HospitalBean hospitalBean = stsb.getHospital();
			if(hospitalBean != null){
				hCode = hospitalBean.getHospitalCode();
				if(hCode != null && hCode.length() > 0){
					final int len = hCode.length();
					hCode = hCode.substring(len - 5, len);
				}
			}
			final int allPerson = Integer.parseInt(stsb.getCountAllPerson().toString());
			final List<SummaryRepDataBean> lstSummaryDetails = this.summaryRepService.getDetailsByRepNo(hCode, fd, td, 0, allPerson, stsb.getAuthType());
			return new ModelAndView("excelViewByRep", "listSummaryDetails", lstSummaryDetails);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
